package com.osi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.osi.bean.Course;
import com.osi.response.R;
import com.osi.service.CourseService;
import com.osi.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lixin
 * @date 2021/2/8 17:24
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentCourseService studentCourseService;

    /**
     * 课程信息列表
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam Map<String, Object> params, ModelMap model){

        Course course = BeanUtil.toBean(params, Course.class);

        List<Course> courseList = courseService.queryList(course);
        model.addAttribute("courseList", courseList);
        return "/admin/course/list";
    }

    /**
     * 课程信息列表
     * @param model
     * @return
     */
    @GetMapping("/query")
    public String query(@RequestParam Map<String, Object> params, ModelMap model){

        Course course = BeanUtil.toBean(params, Course.class);

        List<Course> courseList = courseService.queryCourseList(course);
        model.addAttribute("courseList", courseList);
        return "/student/course/list";
    }


    @GetMapping("/info/{courseId}")
    public String info(@PathVariable("courseId") Integer courseId, ModelMap model){
        Course course = courseService.getById(courseId);
        model.addAttribute("course", course);
        return "/admin/course/edit";
    }


    @PostMapping("/edit")
    public String edit(@RequestParam Map<String, Object> params, ModelMap model){
        Course course = BeanUtil.toBean(params, Course.class);

        if(courseService.updateById(course)) {
            return "redirect:/course/list";
        }
        model.addAttribute("course", course);
        return "redirect:/course/list";
    }


    /**
     * 课程选择
     * @param model
     * @return
     */
    @PostMapping("/select")
    @ResponseBody
    public R select(@RequestParam("courseId") Integer courseId, ModelMap model){

        return studentCourseService.selectCourse(courseId);
    }

    @PostMapping("/quit")
    @ResponseBody
    public R quit(@RequestParam("courseId") Integer courseId, ModelMap model){

        return studentCourseService.quitCourse(courseId);
    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam("courseId") Integer courseId, ModelMap model){

        courseService.removeById(courseId);
        return R.ok("删除成功");
    }

    @RequestMapping("/toAddPage")
    public String toAddPage(ModelMap model){

        return "/admin/course/add";
    }

    @PostMapping("/add")
    public String add(@RequestParam Map<String, Object> params, ModelMap model){

        Course course = BeanUtil.toBean(params, Course.class);
        courseService.save(course);
        return "redirect:/course/list";
    }



}
